//
//  WKDetailView.m
//  WKDemo
//
//  Created by wangzhaohui-Mac on 14-8-6.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKDetailView.h"
#import "WKStatusFrame.h"
#import "WKStatusToolbar.h"
#import "UIImageView+WebCache.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

@interface WKDetailView ()
/**头像*/
@property (nonatomic, weak) UIImageView *iconView;
/**昵称*/
@property (nonatomic, weak) UILabel *nameLabel;
/**时间*/
@property (nonatomic, weak) UILabel *timeLabel;
/**正文*/
@property (nonatomic, weak) UILabel *textlabel;
/**图片*/
@property (nonatomic, weak) UIImageView *image;

/**gif图标*/
@property (nonatomic, weak) UIImageView *gifView;
/**cell工具条*/
@property (nonatomic,strong) WKStatusToolbar *toolbar;

@end

@implementation WKDetailView

#pragma mark - 初始化方法
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.userInteractionEnabled = YES;
        //1,添加头像
        UIImageView *iconView = [[UIImageView alloc] init];
        iconView.clipsToBounds = YES;
        iconView.layer.cornerRadius = 10;
        [self addSubview:iconView];
        self.iconView = iconView;
        
        //2,添加昵称
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        //3,添加时间
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.font = [UIFont systemFontOfSize:10];
        timeLabel.textColor = [UIColor orangeColor];
        [self addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        //4,添加正文
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.font = [UIFont systemFontOfSize:14];
        textLabel.numberOfLines = 0;
        [self addSubview:textLabel];
        self.textlabel = textLabel;
        
        //5.添加图片
        UIImageView *imageview = [[UIImageView alloc] init];
        
        imageview.userInteractionEnabled = YES;
        //
        imageview.contentMode = UIViewContentModeScaleAspectFill;
        imageview.clipsToBounds = YES;
        
        //给gif配图添加gif图标
        UIImage *image = [UIImage imageWithName:@"timeline_image_gif"];
        UIImageView *GIFImageView = [[UIImageView alloc] initWithImage:image];
        [imageview addSubview:GIFImageView];
        self.gifView = GIFImageView;
        
        //创建手势监听器
        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] init];

        [imageview addGestureRecognizer:tgr];
        [tgr addTarget:self action:@selector(test:)];
        
        [self addSubview:imageview];
        self.image = imageview;
    
        //6,添加cell的工具条
        WKStatusToolbar *toolbar = [[WKStatusToolbar alloc] init];
        [self addSubview:toolbar];
        self.toolbar = toolbar;
        
    }
    return self;
}

- (void)setStatusFrame:(WKStatusFrame *)statusFrame
{
    _statusFrame = statusFrame;
    
    WKResult *result = statusFrame.result;
    
    //1,头像
//    [self.iconView setImageWithURL:[NSURL URLWithString:result.profile_image] placeholderImage:[UIImage imageNamed:@"placeholderImage2"]];
    
    UIImageView *imageV = [[UIImageView alloc]init];
    [imageV setImageWithURL:[NSURL URLWithString:result.profile_image] placeholderImage:[UIImage imageNamed:@"placeholderImage1"]];
     UIImage *oldImage = imageV.image;

     self.iconView.image = [UIImage circleImageWithName:oldImage borderWidth:1 borderColor:[UIColor black75PercentColor]];
    self.iconView.frame = statusFrame.iconFrame;
    
    //2,昵称
    self.nameLabel.text = result.name;
    self.nameLabel.frame = statusFrame.nameFrame;
    //3,时间
    self.timeLabel.text = result.passtime;
    self.timeLabel.frame = statusFrame.timeFrame;
    //4,正文
    self.textlabel.text = result.text;
    self.textlabel.frame = statusFrame.textFrame;
    
    //    WKLog(@"result.is_gif===>>%@",result.is_gif);
    //5,图片
    if ([result.is_gif intValue]) {
        self.gifView.hidden = NO;
        [self.image setImageWithURL:[NSURL URLWithString:result.gifFistFrame] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    }else
    { self.gifView.hidden = YES;
        //图片
        [self.image setImageWithURL:[NSURL URLWithString:result.image0] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    }
    self.image.frame = statusFrame.imageFrame;
    
    //6,工具条
    self.toolbar.frame = statusFrame.toobaeFrame;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.gifView.x = self.image.width - self.gifView.width;
    self.gifView.y = self.image.height - self.gifView.height;
    
    
}

#pragma mark - 图片浏览器
- (void)test:(UITapGestureRecognizer *)tgr
{
    //创建图片浏览
    MJPhotoBrowser *photoBrowser = [[MJPhotoBrowser alloc] init];
    
    NSMutableArray *photos = [NSMutableArray array];
 
    MJPhoto *photo = [[MJPhoto alloc] init];
    
    if (self.statusFrame.result.is_gif) {
    //配图的加载路径
    photo.url = [NSURL URLWithString:self.statusFrame.result.image0];
    }else
    {
        photo.image = self.image.image;
    }
    //配图的加载路径
//    photo.url = [NSURL URLWithString:self.statusFrame.result.image2];
//    photo.save = YES;
    //配图的显示
//    photo.image = self.image.image;
    photo.srcImageView = self.image;
    //设置配图需要显示的imageView的来源
    [photos addObject:photo];
  
    photoBrowser.photos = photos;
  
    //显示
    [photoBrowser show];
}

@end
